package com.tczs.dispatch;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class CollectAction {

    public static String URL_PATTER = "/api/";
    public static Map<Class,Object> objects = new HashMap<>();
    public static final Map<String,Handler> handlers = new HashMap<>();

    public static void setObjects(Map<Class,Object> map){
        objects = map;
    }

    /**
     * 加载所有类的方法
     */
    public static void loadHandlers() {
        Set<Class> keySet = objects.keySet();
        for(Class clazz : keySet) {
            Method[] methods = clazz.getDeclaredMethods();
            for (Method method : methods) {
                Handler handler = new Handler();
                handler.setClazz(clazz);
                handler.setMethod(method);
                handler.setReturnType(method.getReturnType());
                Parameter[] parameters = method.getParameters();
                handler.setParameters(parameters);
                handlers.put(URL_PATTER + method.getName(), handler);
            }
        }
    }
}
